package com.pactera.madp.cp.controller.oms.employee;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.constant.DeptConstants;
import com.pactera.madp.cp.api.dto.emp.EmployeeCustomerDataDTO;
import com.pactera.madp.cp.api.dto.emp.EmployeeListDTO;
import com.pactera.madp.cp.api.dto.emp.EmployeeTendencyChartDTO;
import com.pactera.madp.cp.api.entity.WorkDeptEntity;
import com.pactera.madp.cp.api.feign.RemoteDeptService;
import com.pactera.madp.cp.api.vo.employee.*;
import com.pactera.madp.cp.service.IWorkDeptService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import com.pactera.madp.cp.service.IWorkEmployeeStatisticService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * EmployeeStatisticsController
 *
 * @author jun
 * @version 1.0
 * 2021/7/15 14:31
 **/
@RestController
@RequestMapping("/statistics")
@Api(value = "statistics",tags = "成员统计")
@AllArgsConstructor
public class EmployeeStatisticsController {

    private final IWorkEmployeeStatisticService employeeStatisticService;
    private final RemoteDeptService remoteDeptService;
    private final IWorkEmployeeService employeeService;
    private final IWorkDeptService workDeptService;

    @GetMapping
    @ApiOperation(value = "成员统计基础信息")
    public R<EmployeeCustomerDataVO> getCustomerData(EmployeeCustomerDataDTO dto){
        return R.ok(employeeStatisticService.getCustomerData(dto));
    }

    @GetMapping("/getStatisticsData/{deptId}")
    @ApiOperation(value = "华融湘江成员统计基础信息")
    public R<EmployeeStatisticsDataVO> getStatisticsData(EmployeeCustomerDataDTO dto, @PathVariable("deptId")Integer deptId) {
        R r = remoteDeptService.getWxDeptId(deptId);
        Integer wxDeptId = (Integer) r.getData();
        EmployeeStatisticsDataVO entity = employeeStatisticService.getStatisticsDatas(dto, deptId, wxDeptId);
        return R.ok(entity);
    }

    @GetMapping("/getStatisticsDataPage/{deptId}")
    @ApiOperation(value = "华融湘江成员统计分页查询")
    public R getStatisticsDataPage(EmployeeCustomerDataDTO dto, @PathVariable("deptId")Integer deptId, Page page, String name) {
        R r = remoteDeptService.getWxDeptId(deptId);
        Integer wxDeptId = (Integer) r.getData();
        WorkDeptEntity workDeptEntity = workDeptService.getOne(
                Wrappers.<WorkDeptEntity>lambdaQuery()
                .select(WorkDeptEntity::getLevel)
                .eq(WorkDeptEntity::getWxDepartmentId, wxDeptId)
        );
        Integer level = workDeptEntity.getLevel();
        if (level < DeptConstants.SUB_BRANCH_LEVEL) {
            LinkedHashMap pageResult = (LinkedHashMap) remoteDeptService.getChildrenPage(deptId, page).getData();
            Page<EmployeeStatisticsDataVO> employeeStatisticsDataVOs = JSON.parseObject(JSON.toJSONString(pageResult), new TypeReference<Page<EmployeeStatisticsDataVO>>() {});
            // 二期优化
            employeeStatisticsDataVOs.getRecords().forEach(s -> {
                EmployeeStatisticsDataVO entity1 = employeeStatisticService.getStatisticsDatas(dto, s.getId(), Integer.parseInt(s.getCode()));
                s.setEmpTotal(entity1.getEmpTotal());
                s.setRoomTotal(entity1.getRoomTotal());
                s.setEmpOnTotal(entity1.getEmpOnTotal());
                s.setEmpLeaveTotal(entity1.getEmpLeaveTotal());
                s.setContactTotal(entity1.getContactTotal());
                s.setContactLossTotal(entity1.getContactLossTotal());
                // 此时name存的机构号org_code
                s.setCode(s.getName());
                s.setName(entity1.getName());
            });
            return R.ok(employeeStatisticsDataVOs);
        } else {
            LinkedHashMap pageResult = (LinkedHashMap) remoteDeptService.getUserPage(name, deptId, page).getData();
            Page<EmployeeStatisticsDataVO> employeeStatisticsDataVOs = JSON.parseObject(JSON.toJSONString(pageResult), new TypeReference<Page<EmployeeStatisticsDataVO>>() {});
            employeeStatisticsDataVOs.getRecords().forEach(s -> {
                EmployeeStatisticsDataVO entity1 = employeeStatisticService.getStatisticsDatas(dto, s.getCode());
                s.setCode(entity1.getCode());
                s.setName(entity1.getName());
                s.setEmpTotal(entity1.getEmpTotal());
                s.setRoomTotal(entity1.getRoomTotal());
                s.setEmpOnTotal(entity1.getEmpOnTotal());
                s.setEmpLeaveTotal(entity1.getEmpLeaveTotal());
                s.setContactTotal(entity1.getContactTotal());
                s.setContactLossTotal(entity1.getContactLossTotal());
            });
            return R.ok(employeeStatisticsDataVOs);
        }
    }

    @GetMapping("/getStatisticsMessageData/{deptId}")
    @ApiOperation(value = "华融湘江成员统计消息统计")
    public R<EmployeeCustomerDataVO> getStatisticsMessageData(EmployeeCustomerDataDTO dto, @PathVariable("deptId")Integer deptId) {
        R r = remoteDeptService.getWxDeptId(deptId);
        Integer wxDeptId = (Integer) r.getData();
        EmployeeCustomerDataVO entity = employeeStatisticService.getCustomerDataByDept(dto, deptId, wxDeptId);
        return R.ok(entity);
    }

    @GetMapping("/getStatisticsMessageDataPage/{deptId}")
    @ApiOperation(value = "华融湘江成员统计消息统计分页查询")
    public R getStatisticsMessageDataPage(EmployeeCustomerDataDTO dto, @PathVariable("deptId")Integer deptId, Page page, String name) {
        R r = remoteDeptService.getWxDeptId(deptId);
        Integer wxDeptId = (Integer) r.getData();
        WorkDeptEntity workDeptEntity = workDeptService.getOne(
                Wrappers.<WorkDeptEntity>lambdaQuery()
                        .select(WorkDeptEntity::getLevel)
                        .eq(WorkDeptEntity::getWxDepartmentId, wxDeptId)
        );
        Integer level = workDeptEntity.getLevel();
        if (level < DeptConstants.SUB_BRANCH_LEVEL) {
            LinkedHashMap pageResult = (LinkedHashMap) remoteDeptService.getChildrenPage(deptId, page).getData();
            Page<EmployeeCustomerDataVO> vos = JSON.parseObject(JSON.toJSONString(pageResult), new TypeReference<Page<EmployeeCustomerDataVO>>() {});
            vos.getRecords().forEach(s -> {
                EmployeeCustomerDataVO entity1 = employeeStatisticService.getCustomerDataByDept(dto, s.getId(), Integer.parseInt(s.getCode()));
                s.setAvgReplyTime(entity1.getAvgReplyTime());
                s.setChatCnt(entity1.getChatCnt());
                s.setMessageCnt(entity1.getMessageCnt());
                s.setReplyPercentage(entity1.getReplyPercentage());
                // 此时name存的机构号org_code
                s.setCode(s.getName());
                s.setName(entity1.getName());
            });
            return R.ok(vos);
        } else {
            LinkedHashMap pageResult = (LinkedHashMap) remoteDeptService.getUserPage(name, deptId, page).getData();
            Page<EmployeeCustomerDataVO> vos = JSON.parseObject(JSON.toJSONString(pageResult), new TypeReference<Page<EmployeeCustomerDataVO>>() {});
            vos.getRecords().forEach(s -> {
                EmployeeCustomerDataVO entity1 = employeeStatisticService.getCustomerDataByUser(dto, s.getCode());
                s.setAvgReplyTime(entity1.getAvgReplyTime());
                s.setChatCnt(entity1.getChatCnt());
                s.setMessageCnt(entity1.getMessageCnt());
                s.setReplyPercentage(entity1.getReplyPercentage());
                s.setCode(entity1.getCode());
                s.setName(entity1.getName());
            });
            return R.ok(vos);
        }
    }

    @GetMapping("/top")
    @ApiOperation(value = "成员客户数top10")
    public R<EmployeeCustomerDataXYVO> getTop10Employee(){
        return R.ok(employeeStatisticService.getTop10Employee());
    }

    @GetMapping("/tendency")
    @ApiOperation(value = "趋势图")
    public R<List<EmployeeStaticsTendencyVO>> getTendencyChart(EmployeeTendencyChartDTO dto) {
        Integer type = dto.getType();
        switch (type) {
            case 1:
            case 2:
            case 3:
            case 4:
                return R.ok(employeeStatisticService.getMessageCount(dto));
            case 5:
                return R.ok(employeeStatisticService.getEmpTotal(dto));
            case 6:
                return R.ok(employeeStatisticService.getEmpOnTotal(dto));
            case 7:
                return R.ok(employeeStatisticService.getEmpLeaveTotal(dto));
            case 8:
                return R.ok(employeeStatisticService.getContactTotal(dto));
            case 9:
                return R.ok(employeeStatisticService.getContactLossTotal(dto));
            case 10:
                return R.ok(employeeStatisticService.getRoomTotal(dto));
        }
        return R.failed("查询字段类型不在1-10内");
    }


    @GetMapping("/emp")
    @ApiOperation(value = "根据员工统计")
    public R<Page<List<EmployeeListVO>>> getEmployeeList(EmployeeListDTO dto, Page page){
        return R.ok(employeeStatisticService.getEmployeeList(dto,page));
    }

    @GetMapping("/details")
    @ApiOperation(value = "成员详情-基本信息")
    public R<EmployeeDetailsVO> getDetails(Integer employeeId){
        return R.ok(employeeStatisticService.getDetails(employeeId));
    }


    @GetMapping("/details-list")
    @ApiOperation(value = "成员详情-客户列表")
    public R<List<EmployeeContactListVO>> getContactList(Integer employeeId){
        return R.ok(employeeStatisticService.getContactList(employeeId));
    }


    @GetMapping("/page")
    @ApiOperation(value = "按分页列表查询")
    public R<Page<EmployeeTendencyChartVO>> getPageList(EmployeeTendencyChartDTO dto,Page page){
        return R.ok(employeeStatisticService.getPageList(dto,page));
    }

}
